<template>
	<cl-crud ref="Crud">
		<cl-row>
			<!-- 刷新按钮 -->
			<cl-refresh-btn />
			<cl-export-btn :columns="Table?.columns" />
			<!-- 新增按钮 -->
			<!-- <cl-add-btn /> -->
			<!-- 删除按钮 -->
			<!-- <cl-multi-delete-btn /> -->
			<cl-flex1 />
			<!-- 条件搜索 -->
			<cl-search ref="Search" />
		</cl-row>

		<cl-row>
			<!-- 数据表格 -->
			<cl-table ref="Table" />
		</cl-row>

		<cl-row>
			<cl-flex1 />
			<!-- 分页控件 -->
			<cl-pagination />
		</cl-row>

		<!-- 新增、编辑 -->
		<cl-upsert ref="Upsert" />
	</cl-crud>
</template>

<script lang="ts" setup>
defineOptions({
	name: 'class-trainLog'
});
import { useDict } from '/$/dict';
import { useCrud, useTable, useUpsert, useSearch } from '@cool-vue/crud';
import { useCool } from '/@/cool';
import { useI18n } from 'vue-i18n';
import { reactive } from 'vue';

const { dict } = useDict();
const { service } = useCool();
const { t } = useI18n();

// 选项
const options = reactive({
	type: [
		{ label: t('购买'), value: 0, type: 'success' },
		{ label: t('消耗'), value: 1, type: 'danger' }
		// { label: t('充值'), value: 2, type: 'success' },
		// { label: t('赠送'), value: 3, type: 'success' }
	]
});

// cl-upsert
const Upsert = useUpsert({
	items: [
		{
			label: t('选择学生'),
			prop: 'studentId',
			component: { name: 'el-input', props: { clearable: true } },
			span: 12,
			required: true
		},
		{
			label: t('选择班级'),
			prop: 'classId',
			component: { name: 'el-input', props: { clearable: true } },
			span: 12,
			required: true
		},
		{
			label: t('时间'),
			prop: 'time',
			component: {
				name: 'el-date-picker',
				props: { type: 'datetime', valueFormat: 'YYYY-MM-DD HH:mm:ss' }
			},
			span: 12,
			required: true
		},
		{
			label: t('类型'),
			prop: 'type',
			component: { name: 'el-radio-group', options: options.type },
			value: 0,
			required: true
		},
		{
			label: t('次数'),
			prop: 'count',
			hook: 'number',
			component: { name: 'el-input-number', props: { min: 0 } },
			span: 12,
			required: true
		},
		{
			label: t('选择操作人'),
			prop: 'operatorId',
			component: { name: 'el-input', props: { clearable: true } },
			span: 12,
			required: true
		},
		{
			label: t('备注'),
			prop: 'remark',
			component: {
				name: 'el-input',
				props: { type: 'textarea', rows: 4 }
			}
		}
	]
});

// cl-table
const Table = useTable({
	columns: [
		// { type: 'selection' },
		{ label: t('门店'), prop: 'classShopId', dict: dict.get('Shop'), minWidth: 140 },
		{ label: t('学生ID'), prop: 'studentId', minWidth: 140 },
		{ label: t('姓名'), prop: 'studentName', minWidth: 140 },
		// { label: t('班级ID'), prop: 'classId', minWidth: 140 },
		{
			label: t('时间'),
			prop: 'time',
			minWidth: 170,
			sortable: 'custom',
			component: { name: 'cl-date-text' }
		},
		{ label: t('类型'), prop: 'type', minWidth: 120, dict: options.type },
		{ label: t('训练次数'), prop: 'trainCount', minWidth: 140, sortable: 'custom' },
		{ label: t('赠送次数'), prop: 'giftCount', minWidth: 140, sortable: 'custom' },
		{ label: t('操作人'), prop: 'operatorName', minWidth: 140 }
		// { label: t("次数"), prop: "count", minWidth: 140, sortable: "custom" },
		// { label: t("操作人ID"), prop: "operatorId", minWidth: 140 },
		// {
		// 	label: t('备注'),
		// 	prop: 'remark',
		// 	showOverflowTooltip: true,
		// 	minWidth: 200
		// },
		// {
		// 	label: t('创建时间'),
		// 	prop: 'createTime',
		// 	minWidth: 170,
		// 	sortable: 'desc',
		// 	component: { name: 'cl-date-text' }
		// },
		// {
		// 	label: t('更新时间'),
		// 	prop: 'updateTime',
		// 	minWidth: 170,
		// 	sortable: 'custom',
		// 	component: { name: 'cl-date-text' }
		// },
		// { type: 'op', buttons: ['edit', 'delete'] }
	]
});

// cl-search
const Search = useSearch({
	items: [
		{
			prop: 'studentName',
			component: {
				name: 'el-input',
				props: { placeholder: '请输入姓名' }
			}
		},
        {
			prop: 'studentPhone',
			component: {
				name: 'el-input',
				props: { placeholder: '请输入电话' }
			}
		},
        {
			prop: 'classShopId',
			component: {
				name: 'cl-select',
				props: { options: dict.get('Shop'), placeholder: '门店' }
			}
		}
	]
});

// cl-crud
const Crud = useCrud(
	{
		service: service.class.trainLog
	},
	app => {
		app.refresh();
	}
);


// 刷新
function refresh(params?: any) {
	Crud.value?.refresh(params);
}
</script>
